// pages/weather/weather.js
Page({
  data: {
    all: {},
    location: '',
    region: []
  },
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
    console.log(this.data.region.toString())
    this.getLocation()
  },
  getLocation() {
    let _this = this
    wx.request({
      url: 'https://restapi.amap.com/v3/geocode/geo?',
      data: {
        key: '2b8e8119dc262a8e1af2f57d9a1552f3',
        address: this.data.region.toString()
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(_this.data.location)
        _this.setData({
          location: res.data.geocodes[0].location
        })
        _this.getWeather()
      }
    })
  },
  getWeather() {
    let _this = this
    wx.request({
      url: 'https://devapi.qweather.com/v7/weather/now?',
      data: {
        key: '90a53ba77d9b42acaf309c2aed4a6449',
        location: _this.data.location
      },
      success(rel) {
        console.log(rel)
        _this.setData({
          all: rel.data.now,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res)
        that.setData({
          location:res.longitude+','+res.latitude
        })
        that.getWeather()
        wx.request({
          url: 'https://restapi.amap.com/v3/geocode/regeo?',
          data: {
            key: '2b8e8119dc262a8e1af2f57d9a1552f3',
            location: that.data.location
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            console.log('地理位置',res)
            let arr = res.data.regeocode.addressComponent
            that.setData({
              region: [arr.province,arr.city,arr.district]
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})